#include <Arduino.h>
#include <WiFi.h>        // 引入WiFi库文件，用于连接WiFi网络
#include "WiFiAirlink.h" // 引入WiFi库文件，用于连接WiFi网络
#include <Wire.h>        // 引入Wire库文件，用于I2C通信
#include "PCF8574.h"     // 引入PCF8574库文件，用于控制I2C扩展IO
PCF8574 pcf8574(0x20);   // 初始化PCF8574对象，传入I2C地址
PCF8574 pcf85741(0x21);  // 初始化PCF8574对象，传入I2C地址
PCF8574 pcf85742(0x22);  // 初始化PCF8574对象，传入I2C地址
PCF8574 pcf85743(0x23);  // 初始化PCF8574对象，传入I2C地址
char DEV_ChipID[16];     // 设备ID
void airlink();          // 声明airlink函数，用于连接MQTT服务器
uint8_t receivedData[24];
uint8_t key[4] = {0x00, 0x00, 0x00, 0x00};
bool control = false;
uint8_t Equipment_status_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x02, 0x00, 0x01}; // 设备状态机，用于控制设备状态的数组
uint8_t Relay_1_state_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x02, 0x00, 0x00};
uint8_t Relay_state_machine[12] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x06, 0x08, 0x00, 0x00, 0x00, 0x00};
uint8_t Relay_1_control_machine[10] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x02, 0x00, 0x00};
uint8_t Relay_control_machine[12] = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00, 0x00};
uint8_t error[] = {0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
void led_blink(int num)
{
  for (int i = 0; i < num; i++)
  {
    digitalWrite(10, HIGH);
    vTaskDelay(200);
    digitalWrite(10, LOW);
    vTaskDelay(200);
  }
}
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, 18, 19); // 初始化串口1，设置波特率为9600, 数据位8, 无校验位, 停止位1, RX引脚为GPIO18, TX引脚为GPIO19
  Serial1.setTimeout(10);                  // 关键！修改readBytes的超时时间（原默认1000ms，改为10ms减少阻塞）
  Wire.begin(5, 4);
  for (int i = 0; i < 8; i++)
  {
    pcf8574.pinMode(i, OUTPUT);
  }
  if (pcf8574.begin())
  {
    Serial.println("OK");
  }
  else
  {
    Serial.println("KO");
  }
  for (int i = 0; i < 8; i++)
  {
    pcf8574.digitalWrite(i, LOW);
  }

  for (int i = 0; i < 8; i++)
  {
    pcf85741.pinMode(i, OUTPUT);
  }
  if (pcf85741.begin())
  {
    Serial.println("OK");
  }
  else
  {
    Serial.println("KO");
  }
  for (int i = 0; i < 8; i++)
  {
    pcf85741.digitalWrite(i, LOW);
  }

  for (int i = 0; i < 8; i++)
  {
    pcf85742.pinMode(i, OUTPUT);
  }
  if (pcf85742.begin())
  {
    Serial.println("OK");
  }
  else
  {
    Serial.println("KO");
  }
  for (int i = 0; i < 8; i++)
  {
    pcf85742.digitalWrite(i, LOW);
  }

  for (int i = 0; i < 8; i++)
  {
    pcf85743.pinMode(i, OUTPUT);
  }
  if (pcf85743.begin())
  {
    Serial.println("OK");
  }
  else
  {
    Serial.println("KO");
  }
  for (int i = 0; i < 8; i++)
  {
    pcf85743.digitalWrite(i, LOW);
  }

  pinMode(10, OUTPUT);
  uint32_t chipId = 0;
  for (int i = 0; i < 17; i = i + 8)
  {
    chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
  }
  snprintf(DEV_ChipID, 16, "SLKZ-%06X", chipId);
}

void loop()
{

  if (Serial1.available() > 0)
  {
    Serial1.readBytes(receivedData, 24);
    switch (receivedData[0])
    {
    case 0x00:
      break;
    case 0x03:
      switch (receivedData[3])
      {
      case 0x01:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(0, HIGH);
          key[0] = key[0] + 0x01;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(0, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x02:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(1, HIGH);
          key[0] = key[0] + 0x02;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(1, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x03:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(2, HIGH);
          key[0] = key[0] + 0x04;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(2, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x04:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(3, HIGH);
          key[0] = key[0] + 0x08;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(3, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x05:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(4, HIGH);
          key[0] = key[0] + 0x10;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(4, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x06:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(5, HIGH);
          key[0] = key[0] + 0x20;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(5, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x07:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(6, HIGH);
          key[0] = key[0] + 0x40;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(6, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x08:
        if (receivedData[5])
        {
          pcf8574.digitalWrite(7, HIGH);
          key[0] = key[0] + 0x80;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf8574.digitalWrite(7, LOW);
          key[0] = key[0] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x09:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(0, HIGH);
          key[1] = key[1] + 0x01;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(0, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x0A:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(1, HIGH);
          key[1] = key[1] + 0x02;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(1, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x0B:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(2, HIGH);
          key[1] = key[1] + 0x04;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(2, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x0C:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(3, HIGH);
          key[1] = key[1] + 0x08;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(3, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x0D:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(4, HIGH);
          key[1] = key[1] + 0x10;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(4, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x0E:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(5, HIGH);
          key[1] = key[1] + 0x20;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(5, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x0F:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(6, HIGH);
          key[1] = key[1] + 0x40;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(6, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x10:
        if (receivedData[5])
        {
          pcf85741.digitalWrite(7, HIGH);
          key[1] = key[1] + 0x80;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85741.digitalWrite(7, LOW);
          key[1] = key[1] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x11:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(0, HIGH);
          key[2] = key[2] + 0x01;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(0, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x12:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(1, HIGH);
          key[2] = key[2] + 0x02;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(1, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x13:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(2, HIGH);
          key[2] = key[2] + 0x04;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(2, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x14:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(3, HIGH);
          key[2] = key[2] + 0x08;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(3, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x15:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(4, HIGH);
          key[2] = key[2] + 0x10;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(4, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x16:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(5, HIGH);
          key[2] = key[2] + 0x20;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(5, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x17:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(6, HIGH);
          key[2] = key[2] + 0x40;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(6, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x18:
        if (receivedData[5])
        {
          pcf85742.digitalWrite(7, HIGH);
          key[2] = key[2] + 0x80;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(7, LOW);
          key[2] = key[2] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x19:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(0, HIGH);
          key[3] = key[3] + 0x01;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(0, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x1A:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(1, HIGH);
          key[3] = key[3] + 0x02;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(1, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x1B:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(2, HIGH);
          key[3] = key[3] + 0x04;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(2, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x1C:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(3, HIGH);
          key[3] = key[3] + 0x08;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(3, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x1D:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(4, HIGH);
          key[3] = key[3] + 0x10;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(4, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x1E:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(5, HIGH);
          key[3] = key[3] + 0x20;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(5, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x1F:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(6, HIGH);
          key[3] = key[3] + 0x40;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(6, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;
      case 0x20:
        if (receivedData[5])
        {
          pcf85743.digitalWrite(7, HIGH);
          key[3] = key[3] + 0x80;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        else
        {
          pcf85742.digitalWrite(7, LOW);
          key[3] = key[3] + 0x00;
          Serial1.write(Relay_1_control_machine, sizeof(Relay_1_control_machine));
          led_blink(2);
        }
        break;

      case 0x21:
        for (int i = 0; i < 8; i++)
        {
          if (receivedData[5] & (1 << i))
          {
            pcf8574.digitalWrite(i, HIGH);
          }
          else
          {
            pcf8574.digitalWrite(i, LOW);
          }
        }
        for (int i = 0; i < 8; i++)
        {
          if (receivedData[7] & (1 << i))
          {
            pcf85741.digitalWrite(i, HIGH);
          }
          else
          {
            pcf85741.digitalWrite(i, LOW);
          }
        }
        for (int i = 0; i < 8; i++)
        {
          if (receivedData[9] & (1 << i))
          {
            pcf85742.digitalWrite(i, HIGH);
          }
          else
          {
            pcf85742.digitalWrite(i, LOW);
          }
        }
        for (int i = 0; i < 8; i++)
        {
          if (receivedData[11] & (1 << i))
          {
            pcf85743.digitalWrite(i, HIGH);
          }
          else
          {
            pcf85743.digitalWrite(i, LOW);
          }
        }
        key[0] = receivedData[5];
        key[1] = receivedData[7];
        key[2] = receivedData[9];
        key[3] = receivedData[11];
        Relay_control_machine[8] = key[0];
        Relay_control_machine[9] = key[1];
        Relay_control_machine[10] = key[2];
        Relay_control_machine[11] = key[3];
        Serial1.write(Relay_control_machine, sizeof(Relay_control_machine));
        led_blink(2);
        break;
      case 0x43:
        airlink();
        esp_restart();
        break;
      }
      break;
    case 0x06:
      switch (receivedData[3])
      {
      case 0x00:
        Serial1.write(Equipment_status_machine, sizeof(Equipment_status_machine));
        led_blink(2);
        break;
      case 0x22:
        if (key[0] & (1 << 0))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x23:
        if (key[0] & (1 << 1))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x24:
        if (key[0] & (1 << 2))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x25:
        if (key[0] & (1 << 3))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x26:
        if (key[0] & (1 << 4))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x27:
        if (key[0] & (1 << 5))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x28:
        if (key[0] & (1 << 6))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x29:
        if (key[0] & (1 << 7))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x2A:
        if (key[1] & (1 << 0))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x2B:
        if (key[1] & (1 << 1))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x2C:
        if (key[1] & (1 << 2))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x2D:
        if (key[1] & (1 << 3))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x2E:
        if (key[1] & (1 << 4))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x2F:
        if (key[1] & (1 << 5))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x30:
        if (key[1] & (1 << 6))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x31:
        if (key[1] & (1 << 7))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x32:
        if (key[2] & (1 << 0))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x33:
        if (key[2] & (1 << 1))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x34:
        if (key[2] & (1 << 2))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x35:
        if (key[2] & (1 << 3))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x36:
        if (key[2] & (1 << 4))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x37:
        if (key[2] & (1 << 5))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x38:
        if (key[2] & (1 << 6))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x39:
        if (key[2] & (1 << 7))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x3A:
        if (key[3] & (1 << 0))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x3B:
        if (key[3] & (1 << 1))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x3C:
        if (key[3] & (1 << 2))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x3D:
        if (key[3] & (1 << 3))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x3E:
        if (key[3] & (1 << 4))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x3F:
        if (key[3] & (1 << 5))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x40:
        if (key[3] & (1 << 6))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;
      case 0x41:
        if (key[3] & (1 << 7))
        {
          Relay_1_state_machine[9] = 0x01;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        else
        {
          Relay_1_state_machine[9] = 0x00;
          Serial1.write(Relay_1_state_machine, sizeof(Relay_1_state_machine));
        }
        led_blink(2);
        break;

      case 0x42:
        Relay_state_machine[8] = key[0];
        Relay_state_machine[9] = key[1];
        Relay_state_machine[10] = key[2];
        Relay_state_machine[11] = key[3];
        Serial1.write(Relay_state_machine, sizeof(Relay_state_machine));
        led_blink(2);
        break;
      }
      break;
    }
  }
  for (int i = 0; i < 24; i++)
  {
    receivedData[i] = 0x00;
  }
}
void airlink()
{ // 新配网程序
  WiFiAirlink Airlink;
  Airlink.setDebugOutput(false);
  Airlink.setMinimumSignalQuality(30); // 只列出信号质量超过30%的SSID
  Airlink.setConfigPortalTimeout(60);  // 60秒不配网操作则退出
  Airlink.setConnectTimeout(30);
  Airlink.startConfigPortal(DEV_ChipID); // 强制开启配网AP
  vTaskDelay(100);
}
